package com.seari.tunnel.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seari.tunnel.entity.MaintenancePlan;
import com.seari.tunnel.entity.MaintenanceRecord;
import com.seari.tunnel.entity.MaintenanceRecordCost;
import com.seari.tunnel.entity.OperationDevice;
import com.seari.tunnel.entity.OperationDeviceType;
import com.seari.tunnel.entity.OperationSpare;
import com.seari.tunnel.entity.RepairApply;
import com.seari.tunnel.entity.RepairApplyCost;
import com.seari.tunnel.mapper.MaintenanceRecordCostMapper;
import com.seari.tunnel.mapper.MaintenanceRepairMapper;
import com.seari.tunnel.mapper.OperationDeviceMapper;
import com.seari.tunnel.mapper.OperationDeviceTypeMapper;
import com.seari.tunnel.mapper.OperationSpareMapper;
import com.seari.tunnel.mapper.RepairApplyCostMapper;
import com.seari.tunnel.service.IMaintenanceRepairService;

@Service(IMaintenanceRepairService.SERVICE_NAME)
public class MaintenanceRepairServiceImpl implements IMaintenanceRepairService{
	@Autowired
	MaintenanceRepairMapper maintenanceRepairMapper;
	@Autowired
	OperationDeviceMapper operationDeviceMapper;
	@Autowired
	OperationDeviceTypeMapper operationDeviceTypeMapper;
	@Autowired
	OperationSpareMapper operationSpareMapper;
	@Autowired
	MaintenanceRecordCostMapper maintenanceRecordCostMapper;
	@Autowired
	RepairApplyCostMapper repairApplyCostMapper;
	
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	public List<MaintenancePlan> findMaintenancePlan(){
		
		return maintenanceRepairMapper.findMaintenancePlan();
	}
	@Override
	public void addMaintenancePlan(MaintenancePlan maintenancePlan){
		
		maintenanceRepairMapper.addMaintenancePlan(maintenancePlan);
	}
	@Override
	public void updateMaintenancePlan(MaintenancePlan maintenancePlan){
		maintenanceRepairMapper.updateMaintenancePlan(maintenancePlan);
	}
	@Override
	public void deleteMaintenancePlan(MaintenancePlan maintenancePlan){
		maintenanceRepairMapper.deleteMaintenancePlan(maintenancePlan);
	}
	@Override
	public List<MaintenanceRecord> findMaintenanceRecord(){
			
		return maintenanceRepairMapper.findMaintenanceRecord();
	}
	@Transactional
	@Override
	public void addMaintenanceRecord(MaintenanceRecord maintenanceRecord)throws RuntimeException{
		MaintenancePlan maintenancePlan = maintenanceRepairMapper.findMaintenancePlanByMPId(maintenanceRecord.getMp_id());
		maintenanceRepairMapper.addMaintenanceRecord(maintenanceRecord);
			maintenancePlan.setState("2");
		    maintenanceRepairMapper.updateMaintenancePlan(maintenancePlan);
	}
	@Transactional
	@Override
	public void updateMaintenanceRecord(MaintenanceRecord maintenanceRecord)throws RuntimeException{
		MaintenancePlan maintenancePlan = maintenanceRepairMapper.findMaintenancePlanById(Integer.parseInt(maintenanceRecord.getId()));
		if("2".equals(maintenanceRecord.getState())){
			if("1".equals(maintenancePlan.getAround())){
				maintenancePlan.setState("3");
				maintenanceRepairMapper.updateMaintenancePlan(maintenancePlan);
			}else{
				maintenancePlan.setLastTime(sdf.format(new Date()));
				int i = Integer.parseInt(maintenancePlan.getInterval());
				Calendar cal = Calendar.getInstance();
				cal.add(Calendar.DATE, i);
				Date date = cal.getTime();
				maintenancePlan.setNextTime(sdf.format(date));
				maintenancePlan.setState("1");
				maintenanceRepairMapper.updateMaintenancePlan(maintenancePlan);
			}
		}
		maintenanceRepairMapper.updateMaintenanceRecord(maintenanceRecord);
	}
	@Transactional
	@Override
	public void delMaintenanceRecord(MaintenanceRecord maintenanceRecord)throws RuntimeException{
		MaintenancePlan maintenancePlan = maintenanceRepairMapper.findMaintenancePlanById(Integer.parseInt(maintenanceRecord.getId()));
		if("1".equals(maintenancePlan.getAround())){
			maintenancePlan.setState("1");
			maintenanceRepairMapper.updateMaintenancePlan(maintenancePlan);
		}
		List<MaintenanceRecordCost> mrcs = maintenanceRecordCostMapper.findMaintenanceRecordCostByMRID(Integer.parseInt(maintenanceRecord.getId()));
		for(int i=0;i<mrcs.size();i++){
			operationSpareMapper.updateBack(mrcs.get(i).getOs_id(), mrcs.get(i).getCount());
		}
		maintenanceRepairMapper.deleteMaintenanceRecord(maintenanceRecord);
	}
	@Override
	public List<MaintenanceRecord> findMaintenanceRemind(){
		
		return maintenanceRepairMapper.findMaintenanceRemind();
	}
	@Override
	public List<RepairApply> findRepairApply(){
		
		return maintenanceRepairMapper.findRepairApply();
	}
	@Override
	public void addRepairApply(RepairApply repairApply){
		
		maintenanceRepairMapper.addRepairApply(repairApply);
	}
	@Override
	public void deleteRepairApply(RepairApply repairApply)throws RuntimeException{
		List<RepairApplyCost> mrcs = repairApplyCostMapper.findRepairApplyCostByRAID(Integer.parseInt(repairApply.getId()));
		for(int i=0;i<mrcs.size();i++){
			operationSpareMapper.updateBack(mrcs.get(i).getOs_id(), mrcs.get(i).getCount());
		}
		maintenanceRepairMapper.deleteRepairApply(repairApply);
	}
	@Override
	public List<RepairApply> findRepairRecord(){
		
		return maintenanceRepairMapper.findRepairRecord();
	}
	@Override
	public void updateRepairRecord(RepairApply repairRecord){
		maintenanceRepairMapper.updateRepairRecord(repairRecord);
	}
	@Override
	public List<OperationDevice> findOperationDevice() {
		
		return operationDeviceMapper.findOperationDevice();
	}
	@Override
	public void deleteOperationDevice(int id) {
		operationDeviceMapper.deleteOperationDevice(id);
		
	}
	@Override
	public void addOperationDevice(OperationDevice operationDevice) {
		operationDeviceMapper.addOperationDevice(operationDevice);
		
	}
	@Override
	public void updateOperationDevice(OperationDevice operationDevice) {
		operationDeviceMapper.updateOperationDevice(operationDevice);
		
	}
	@Override
	public List<OperationDeviceType> findOperationDeviceType() {
		
		return operationDeviceTypeMapper.findOperationDeviceType();
	}
	@Override
	public List<MaintenancePlan> findUnfinishedMaintenancePlan() {
		
		return maintenanceRepairMapper.findUnfinishedMaintenancePlan();
	}
	@Override
	public OperationSpare findOperationSpareById(int id) {
		
		return operationSpareMapper.findOperationSpareById(id);
	}
	@Transactional
	@Override
	public void addOperationSpareCost(int id, int os_id, String costNum)throws RuntimeException{
		maintenanceRecordCostMapper.addMaintenanceRecordCost(id, os_id, costNum);
		operationSpareMapper.updateCost(os_id,costNum);
	}
	@Transactional
	@Override
	public void addRepairApplyCost(int id, int os_id, String costNum)throws RuntimeException{
		repairApplyCostMapper.addRepairApplyCost(id, os_id, costNum);
		operationSpareMapper.updateCost(os_id,costNum);
	}
	@Override
	public void updateRepairApply(RepairApply repairApply) {
		maintenanceRepairMapper.updateRepairApply(repairApply);
		
	}
	@Override
	public void updateRepairState(RepairApply repairApply) {
		maintenanceRepairMapper.updateRepairState(repairApply);
		
	}
	@Override
	public List<MaintenanceRecord> findFinishMaintenanceRecord() {
		
		return maintenanceRepairMapper.findFinishMaintenanceRecord();
	}
	@Override
	public List<MaintenanceRecordCost> findOSCostById(int id) {
		return maintenanceRecordCostMapper.findMaintenanceRecordCostByMRID(id);
	}
	@Override
	public List<RepairApply> findFinishRepairRecord() {
		return maintenanceRepairMapper.findFinishRepairRecord();
	}
	@Override
	public List<RepairApplyCost> findRACostById(int id) {
		
		return repairApplyCostMapper.findRepairApplyCostByRAID(id);
	}
	@Override
	public List<MaintenancePlan> findMaintenancePlanByDeviceCode(
			String DeviceCode) {
		return maintenanceRepairMapper.findMaintenancePlanByDeviceCode(DeviceCode);
	}
}
